package battleship;

import org.apache.catalina.Session;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.LinkedBlockingQueue;

public class ShipsServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("ShipsServlet post: starting");
    	String userToken= request.getParameter("token");
//        System.out.println("token: " + userToken);
    	int id= (Integer) UserIdTokenHashMap.idTokenMap.get(userToken);
      //  int id = Integer.parseInt(request.getParameter("id"));
//        System.out.println("parsed id: " + id);
        User user = Context.users[id];
        User opponent = Context.users[user.opponentID];
//        System.out.println("done with initial part");

        String ships1 = request.getParameter("ships");
//        System.out.println("got these ships: " + ships1);
        ArrayList<ArrayList<int[]>> shipLocations = new ArrayList<ArrayList<int[]>>();

        // parse the ship coordinates string and create fill shipLocations
        String[] ships = ships1.split("\\.");
        for (String ship : ships) {
//            System.out.println("looking at a ship");
            ArrayList<int[]> ship1 = new ArrayList<int[]>();
            String[] coords = ship.split(";");
            for (String coord : coords) {
                String[] xy = coord.split(",");
                int[] xy1 = new int[2];
                xy1[0] = Integer.parseInt(xy[0]);
                xy1[1] = Integer.parseInt(xy[1]);
                ship1.add(xy1);
//                System.out.println("added the coord " + xy1);
            }
            shipLocations.add(ship1);
            Iterator<int[]> it1 = ship1.iterator();
            while (it1.hasNext()) {
//                System.out.println("the ship has a coord!");
                it1.next();
            }
        }
        Iterator<ArrayList<int[]>> it = shipLocations.iterator();
        while (it.hasNext()) {
//            System.out.println("shipLocations has a ship!");
            Iterator<int[]> it1 = it.next().iterator();
            while (it1.hasNext()) {
//                System.out.println("the ship has a coord!");
                it1.next();
            }
        }
        
        System.out.println("ShipsServlet: done parsing!");
        
        PrintWriter out = response.getWriter();
        response.setContentType("text/plain");
        
        user.donePlacingShips = true;
        user.ships = shipLocations;
        String opponentFinished;
        if (opponent.donePlacingShips) {
            try {
                opponent.waitForOppShips.put(user);
            } catch (Exception e) {
                System.out.println("ShipsServlet exception: " + e.getMessage());
            }
            opponentFinished = "\"opponentFinished\": true";
        } else {
            opponentFinished = "\"opponentFinished\": false";
        }
        String startingPlayer = "\"startingPlayer\": " + user.startingPlayer;
        String reply = "{ " + opponentFinished + ", " + startingPlayer + " }";
        System.out.println("the reply will be " + reply);
        out.print(reply);
        out.close();
        
//        if (user.equals(game.getPlayer1())) {
//            game.player1DonePlacingShips = true;
//            game.player1Ships = shipLocations;
//            if (game.player2DonePlacingShips) {
//                try {
//                    game.opponentFinished.put(user);
//                } catch (Exception e) {
//                    System.out.println(e.getMessage());
//                }
//                out.print("true");
//            } else {
//                out.print(""); // evaluates to false in javascript
//            }
//        } else {
//            game.player2DonePlacingShips = true;
//            game.player2Ships = shipLocations;
//            if (game.player1DonePlacingShips) {
//                try {
//                    game.opponentFinished.put(user);
//                } catch (Exception e) {
//                    System.out.println(e.getMessage());
//                }
//                out.print("true");
//            } else {
//                out.print(""); // evaluates to false in javascript
//            }
//        }

        

        System.out.println("ShipsServlet post: finished");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("ShipsServlet get: starting");
    	String userToken= request.getParameter("token");
    	int id= (Integer) UserIdTokenHashMap.idTokenMap.get(userToken);
      //  int id = Integer.parseInt(request.getParameter("id"));
        User user = Context.users[id];

        try {
            user.waitForOppShips.take();
        } catch (InterruptedException e) {
            System.out.println("ShipsServlet get exception: " + e.getMessage());
        }
        
        System.out.println("ShipsServlet get: finished");
    }
}
